package uno.rocc.framework.security.domain;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import uno.rocc.system.domain.dao.SysUser;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Builder
public class RoccUnoUserDetails implements UserDetails {

    private SysUser sysUser;
    private  List<String> permissionList;
    @JsonIgnore
    private List<SimpleGrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (ObjectUtil.isEmpty(permissionList)) {
            return null;
        }

        if (authorities == null) {
            authorities = permissionList.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    @Override
    public String getUsername() {
        return sysUser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return sysUser.getStatus();
    }

    @Override
    public boolean isAccountNonLocked() {
        return sysUser.getStatus();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return sysUser.getStatus();
    }

    @Override
    public boolean isEnabled() {
        return sysUser.getStatus();
    }
}
